package it.softphone.commons.util;

import java.lang.management.ManagementFactory;

import javax.management.MBeanServer;
import javax.management.ObjectName;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;

/**
 * Utility class to manage log.
 * 
 * Below a working example useful for startup
 * <pre>
 *	log4j.rootCategory=info, stdout
 *		
 *	log4j.appender.stdout=org.apache.log4j.ConsoleAppender
 *	log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
 *	log4j.appender.stdout.threshold=DEBUG
 *	
 *	log4j.category.commons=debug, AD
 *	log4j.appender.AD=org.apache.log4j.RollingFileAppender
 *	log4j.appender.AD.File=commons.log
 *	log4j.appender.AD.MaxFileSize=4000KB
 *	# Keep one backup file
 *	log4j.appender.AD.MaxBackupIndex=1
 *	log4j.appender.AD.layout=org.apache.log4j.PatternLayout
 *	log4j.appender.AD.layout.ConversionPattern=%d{ISO8601} %t %c - %m%n
 * </pre>
 * 
 * 
 */
public class LogUtil {

	public static final String LOGGER_CATEGORY = "commons";
	
	public static final Log logger;
	
	static {
		logger = LogFactory.getLog(LOGGER_CATEGORY);
	
	}

	public static final void initJMX() {
	   
		try {
			//MBeanServer mbs = MBeanServerFactory.createMBeanServer();
			MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
			
			if( mbs==null ) {
				java.util.logging.Logger.getAnonymousLogger().warning( "initJMX platform MBean server is null");
				return;
			}

			org.apache.log4j.jmx.LoggerDynamicMBean mbean = new org.apache.log4j.jmx.LoggerDynamicMBean(Logger.getLogger(LOGGER_CATEGORY));

			ObjectName mbo = new ObjectName(String.format("log4j:logger=%s", LOGGER_CATEGORY));
			
			mbs.registerMBean(mbean, mbo);
/*			
			// Create and Register the top level Log4J MBean
			HierarchyDynamicMBean hdm = new HierarchyDynamicMBean();

			ObjectName mbo = new ObjectName(LOG4J_HIEARCHY_DEFAULT);

			mbs.registerMBean(hdm, mbo);
			
			hdm.addLoggerMBean(LOGGER_CATEGORY);
*/
		} catch (Exception e) {
			Logger.getRootLogger().warn( "initJMX exception", e);
		}
	}

	
	protected LogUtil() {
		
	}
	
}
